define(['zepto'],function(){
	//getToken
	var address=location.search.toString();
	var tokenValue="";
    if(address!=''){
    	tokenValue=/^\?token=(.+)\s*$/.exec(address)[1];
    }
    else{
    	window.location.href="./findPassword.html";
    }
	//pwd
	var elPwd='#pwd';
	var valiPwdFlag=false;
	var defaultPwd="";
	var hideFlag=true;
	var reg1=/^[a-z]{8,}$|^[A-Z]{8,}$|^[0-9]{8,}$/;
	var reg2=/^[a-z]+[A-Z]+[a-zA-Z]*$|^[A-Z]+[a-z]+[a-zA-Z]*$|^[a-zA-Z]+\d+[a-zA-Z0-9]*$|^\d+[a-zA-Z]+[a-zA-Z0-9]*$/;
	var reg3=/\W|_/;
	var $intensionBar=$('.intensionBar');
	
	document.getElementById(elPwd.replace('#','')).addEventListener('input',function(){
		defaultPwd=$(elPwd).val().trim();
		if(defaultPwd!=""){
			$('.firstPwd').removeClass('error');
			$('.info')[0].innerHTML="";
			if(hideFlag){
				$('.intension').removeClass('hide');
				hideFlag=false;
			}
			if(defaultPwd.length>=8){					
				if(defaultPwd.match(reg1)){
					$intensionBar.addClass('low');
					$intensionBar.removeClass('high');
					$intensionBar.removeClass('middle');
					$('.intension').find('em')[0].innerHTML="低";
				}
				if(defaultPwd.match(reg2)){
					$intensionBar.removeClass('low');
					$intensionBar.addClass('middle');
					$intensionBar.removeClass('high');
					$('.intension').find('em')[0].innerHTML="中";
				}
				if(defaultPwd.match(reg3)){
					$intensionBar.removeClass('low');
					$intensionBar.removeClass('middle');
					$intensionBar.addClass('high');
					$('.intension').find('em')[0].innerHTML="高";
				}
				if(defaultPwd.length<=16){
					valiPwdFlag=true;
					return;
				}
				else{
					$('.firstPwd').addClass('error');
					info("<i>密码为8位-16位非空字符</i>");
					valiPwdFlag=false;
				}
			}
			else{
				$intensionBar.addClass('low');
				$intensionBar.removeClass('high');
				$intensionBar.removeClass('middle');
				$('.intension').find('em')[0].innerHTML="低";
				info("<i>密码为8位-16位非空字符</i>");
				valiPwdFlag=false;
			}
		}
		else{
			$('.intension').addClass('hide');
			hideFlag=true;
		}
		validateInfo();
	})

	//pwdMore
	var elPwdMore='#pwdMore';
	var valiPwdMoreFlag=false;
	var pwdMore="";
	document.getElementById(elPwdMore.replace('#','')).addEventListener('input',function(){
		if($(elPwdMore).val().trim()!=""){
			pwdMore=$(elPwdMore).val().toString().trim('');
			if(pwdMore==defaultPwd){
				valiPwdMoreFlag=true;
			}
			else{
				valiPwdMoreFlag=false;
			}			
		}
		validateInfo();
	});
	$(elPwdMore).on('blur',function(event){
		if($(elPwdMore).val().trim()!=""){
			pwdMore=$(elPwdMore).val().toString().trim('');
			if(pwdMore==defaultPwd){
				valiPwdMoreFlag=true;
				$('.info')[0].innerHTML="";
				return;
			}
			else{
				info("<i>两次密码输入不一致</i>");
				valiPwdMoreFlag=false;
			}
		}
	});
	function info(info){
		$('.info')[0].innerHTML=info;
	}
	
	//validate
 	function validateInfo(){
 		if(valiPwdFlag&&valiPwdMoreFlag){
 			$('.complete').removeClass('submitDefault');
 		}
 		else{
 			$('.complete').addClass('submitDefault');
 		}
 	}
 	//
 	var timeID;
 	function clearLoop(){
 		clearInterval(timeID);
 		window.location.href="./login.html";
 	} 
	//complete
	$('.complete').on('click',function(){	
 		if(valiPwdFlag&&valiPwdMoreFlag){
 			//ajax
 			$.ajax({
	            type: 'POST',
	            url: '/mobile/set-password/',
	            data: {password1:defaultPwd,password2:pwdMore,token:tokenValue},
	            dataType: 'json',
	            timeout: 3000,
	            success: function(response){
	              	var status = parseInt(response.code);
		            if(status == 0){
		            	defaultShow();
		            	info("<i>密码修改成功,<em>3</em>s后跳转登录页面</i>");
		            	var secondShow=3;	
		            	timeID=setInterval(function(){
				 			$('.info i').find('em')[0].innerHTML=secondShow--;
				 			if(secondShow==0){
				 				clearLoop();
				 			}
			 			},1000)	           			            	
		            }
		            else{
		            	var failInfo=response.messages.message;
		                info("<i>"+failInfo+"</i>");
		            }
	            },
	            error: function(xhr, type){
	            	info("<i>操作失败请重试</i>");
	            }
        	})
 			//		
 		}
 		else{
 			info("<i>密码输入不正确</i>");
 		}
 	})
 	//init
 	function defaultShow(){
        valiPwdFlag=false;
        valiPwdMoreFlag=false;
        $(elPwd).val("");
        $(elPwdMore).val("");
        validateInfo();
	}
})